<?php

namespace App\HttpController\Api\Admin\Security;

use App\HttpController\Api\Admin\AdminBase;
use App\Lib\Utils;
use App\Model\User\UserModel;
use EasySwoole\Http\Message\Status;
use App\Model\Face\CompareModel;
use EasySwoole\Mysqli\QueryBuilder;

class Quickshot extends AdminBase
{

    public function index()
    {
        $model = new CompareModel();
        $str = $this->raw;
        $model->params = json_decode(Utils::characet($str),true);
        $data = $model->getQuickshotList();
        if (!$data) {
            return $this->writeJson(Status::CODE_BAD_REQUEST, "Server Error".$model->message);
        }

        $this->response()->withHeader('Content-Type', 'application/json;charset=utf-8');
        $this->response()->write(json_encode([
            'code' => 0,
            'data' => $data['lists'],
            'count' => $data['count'],
        ]));
    }

    public function preview()
    {
        $params = $this->params;
        $id = isset($params['id']) ? (int)$params['id'] : 0;

        $model = new CompareModel;
        $data = $model->getOne($id);

        if (!$data) {
            return $this->writeJson(Status::CODE_BAD_REQUEST, "数据不存在");
        }

        $domain = "http://dev.smart_campus_server.com/webroot";
        $data['imgpath'] = $domain.$data['imgpath'];
        $data['capture_time'] = date('Y.m.d H:i:s', $data['capture_time']);

        $this->response()->withHeader('Content-Type', 'application/json;charset=utf-8');
        $this->response()->write(json_encode([
            'code' => 0,
            'data' => $data,
        ]));
    }

    public function upOrDown()
    {
        $model = new CompareModel();
        $str = $this->raw;
        $model->params = json_decode(Utils::characet($str),true);

        $id = isset($model->params['id']) ? (int)$model->params['id'] : 0;
        if ($id <= 0) {
            return $this->toJson(Status::CODE_BAD_REQUEST, [], "参数错误");
        }

        $data = $model->getUpOrDown($id);
        if (!$data) {
            return $this->toJson(Status::CODE_BAD_REQUEST, [], "Server Error".$model->message);
        }

        $this->toJson(0, $data, 'ok');
    }

    public function download()
    {
        global $_W;

        $params = $this->params;
        $id = isset($params['id']) ? (int)$params['id'] : 0;
        if ($id <= 0) {
            return $this->toJson(Status::CODE_BAD_REQUEST, [], "参数错误");
        }

        try {
            $model = new CompareModel();
            $year = date('Y', time());
            $tableName = "sc_compare_{$_W['companyid']}_{$year}";
            $model->tableName($tableName);
            $data = $model->getOne($id);

            if (empty($data)) {
                return $this->toJson(Status::CODE_BAD_REQUEST, [], '数据不存在');
            }

            $file = EASYSWOOLE_ROOT.'/webroot'.$data['imgpath'];
            $imgname = basename($data['imgpath']);
            $fp = fopen($file, 'r');
            $content = fread($fp, filesize($file));
            $this->response()->write($content);
            $this->response()->withHeader('Content-type', 'application/octet-stream');
            $this->response()->withHeader('Content-Disposition', 'attachment;filename="'. $imgname .'"');
            $this->response()->withHeader('Content-Length', filesize($file));
            $this->response()->end();

        } catch(\Exception $e) {
            echo $e->getMessage();
            return $this->writeJson(Status::CODE_BAD_REQUEST, 'Server Error '.$e->getMessage(), []);
        }

        return $this->writeJson(Status::CODE_LIST_OK, 'ok', $data);
    }




}